package com.taptech.doufu.ui.view.choice.base;
import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taptech.doufu.bean.choice.ComicModel;
import com.taptech.doufu.weex.http.TFHttpError;
import com.taptech.doufu.weex.http.TFHttpResponse;
import com.taptech.doufu.weex.http.TFHttpUtil;

import java.util.HashMap;
import java.util.List;

public abstract class ChoiceTopicComicBaseView extends ChoiceTopicBaseView {

    public ChoiceTopicComicBaseView(Context context) {
        super(context);
    }

    public ChoiceTopicComicBaseView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicComicBaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public abstract void updateList(List<ComicModel> list);

    @Override
    public void replaceData(String moreUrl) {
        TFHttpUtil.requestGet(moreUrl, new HashMap(), new TFHttpResponse() {
            @Override
            public void onResponse(JSONObject json, TFHttpError error) {
                if (json != null && json.containsKey("data") &&
                        json.getJSONObject("data")!=null && json.getJSONObject("data").containsKey("list")) {
                    JSONObject jsonObject = json.getJSONObject("data");
                    if (jsonObject.containsKey("moreUrl")) {
                        replaceUrl = jsonObject.getString("moreUrl");
                    }
                    String array = jsonObject.getString("list");
                    List<ComicModel> list = JSON.parseArray(array, ComicModel.class);
                    if (list.size() > 0) {
                        updateList(list);
                    }else{
                        Toast.makeText(getContext(),"无更多数据",Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }
}
